C++ Boolean
⋮
Boolean (or bool) data types are used in C++ to represent logical values. One of two values can be stored in a boolean variable:
actual (logically correct, equal to 1)false (equal to 0 in terms of reasoning)
Key attributes of the C++ data type bool include:
When declaring a boolean variable, the keyword bool is used.
Default Values:
Boolean values are represented by the reserved keywords true and false in C++.
Recollection:
Most implementations use one byte of memory for bools.
Use:
Common applications of boolean variables include logical operations and decision-making (for example, in if statements).
Declaration and Initialization
#include <iostream>
using namespace std;
int main() {
bool isRaining = true; // Declare and initialize
bool isSunny = false; // Another boolean variable
cout << "Is it raining? " << isRaining << endl; // Outputs: 1
cout << "Is it sunny? " << isSunny << endl; // Outputs: 0
return 0;
}
In conditional statements, Boolean values
If, while, and for loops are examples of conditional statements that frequently involve booleans.
For instance:
#include <iostream>
using namespace std;
int main() {
bool isAdult = true;
if (isAdult) {
cout << "You are an adult!" << endl;
} else {
cout << "You are not an adult!" << endl;
}
return 0;
}
Boolean and Logical Operators
C++ provides several logical operators that work with boolean values:
| Operator | Name | Example | Description |
|---|---|---|---|
&& | Logical AND | a && b | Returns true if both a and b are true. |
| ` | ` | Logical OR | |
! | Logical NOT | !a | Returns true if a is false. |
Example: Logical Operations
#include <iostream>
using namespace std;
int main() {
bool isRaining = true;
bool hasUmbrella = false;
if (isRaining && !hasUmbrella) {
cout << "You should stay indoors!" << endl;
}
if (isRaining || hasUmbrella) {
cout << "You can go outside safely!" << endl;
}
return 0;
}
Boolean and Comparison Operators
Boolean expressions often involve comparison operators that return true or false.
| Operator | Name | Example | Description |
|---|---|---|---|
== | Equal to | a == b | Returns true if a is equal to b. |
!= | Not equal to | a != b | Returns true if a is not equal to b. |
> | Greater than | a > b | Returns true if a is greater than b. |
< | Less than | a < b | Returns true if a is less than b. |
>= | Greater than or equal to | a >= b | Returns true if a is greater than or equal to b. |
<= | Less than or equal to | a <= b | Returns true if a is less than or equal to b. |
Example:
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20;
bool result1 = (x > y); // false
bool result2 = (x <= y); // true
cout << "Is x greater than y? " << result1 << endl; // Outputs: 0
cout << "Is x less than or equal to y? " << result2 << endl; // Outputs: 1
return 0;
}
Loop Boolean
It is common practice to utilize boolean values as loop conditions.
For instance, a while loop
#include <iostream>
using namespace std;
int main() {
bool isRunning = true;
int counter = 0;
while (isRunning) {
cout << "Counter: " << counter << endl;
counter++;
if (counter >= 5) {
isRunning = false; // Exit the loop
}
}
return 0;
}
Comments